﻿using System.Security.Claims;
using Microsoft.AspNetCore.Http;

namespace Domain.Infrastructure.HttpContextUser
{
    public class CustomUser : IUser
    {

        private readonly IHttpContextAccessor _accessor;


        public CustomUser(IHttpContextAccessor accessor)
        {
            _accessor = accessor;
        }

        public string GetToken()
        {
            return (_accessor.HttpContext?.Request?.Headers?["Authorization"])?.ToString().Replace("Bearer ", "") ?? "";
        }
        public string? UserID => GetClaimValueByType("jti").FirstOrDefault();

        public string? UserName => _accessor.HttpContext.User.Identity?.Name;

        public string? UserRole => GetClaimValueByType(ClaimTypes.Role).FirstOrDefault();

        public bool IsAdmin
        {
            get
            {
                return GetClaimValueByType(ClaimTypes.Role)?.Any(p => p == "Admin") ?? false;
            }
        }

        public IEnumerable<Claim> GetClaimsIdentity()
        {
            var claims = _accessor.HttpContext.User.Claims.ToList();
            return claims;
        }
        public List<string?> GetClaimValueByType(string ClaimType)
        {

            return (from item in GetClaimsIdentity()
                where item.Type == ClaimType
                select item.Value).ToList();

        }
    }
}
